﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.18
 * 描 述：凭证列表
 */
var refreshGirdData; // 更新数据

var params = {};

var bootstrap = function ($, learun) {
    "use strict";

    var page = {
        init: function () {
            $("#gridtable").height($(window).height() - 230);
            page.bind();

        },
        bind: function () {
            // 查询
            $('#btn_search').on('click', function () {
                var sql = $("#sql").val();
                top.layer.close(top.layer.index);
                page.initGrid(sql);
            });
            // 执行
            $('#lr_excute').on('click', function () {
                var sql = $("#sql").val();
                learun.layerConfirm('是否确认执行该脚本?', function (res) {
                    if (res) {
                        learun.postForm(config.base_server() + 'dbs/excute?sql=' + sql + '&access_token=' + config.getToken().access_token, {}, function () {
                            top.layer.alert("执行脚本成功");
                        });
                    }
                })
            });
            // 备份
            $('#lr_backup').on('click', function () {
                learun.layerConfirm('是否确认备份数据库?', function (res) {
                    if (res) {
                        var params = {};
                        params.access_token = config.getToken().access_token;
                        learun.download({
                            url: config.base_server() + 'dbs/backup',
                            param: params
                        });
                        top.layer.close(top.layer.index);
                    }
                })
            });
            // 还原
            $('#lr_restore').on('click', function () {
                learun.layerForm({
                    id: 'UploadifyForm',
                    title: '上传数据库文件',
                    url: config.webportal_server + 'pages/system/db/uploadifyForm.html?access_token=' + config.getToken().access_token,
                    width: 600,
                    height: 400,
                    callBack: function (id) {
                        return top[id].acceptClick(function (datas) {
                            if (datas) {
                                refreshGirdData();
                            }
                        });
                    }
                });
            });

            // 上传升级包
            $('#lr_upgrade').on('click', function () {
                learun.layerForm({
                    id: 'UploadifyForm',
                    title: '上传升级包',
                    url: config.webportal_server + 'pages/system/db/uploadifyForm2.html?access_token=' + config.getToken().access_token,
                    width: 600,
                    height: 400,
                    callBack: function (id) {
                        return top[id].acceptClick(function (datas) {
                            if (datas) {
                                refreshGirdData();
                            }
                        });
                    }
                });
            });
        },
        initGrid: function (sql) {
            learun.httpAsync('GET', config.base_server() + 'dbs/columnNames?sql=' + sql + '&access_token=' + config.getToken().access_token, {}, function (data) {
                var headData = [];
                for (var i = 0, l = data.length; i < l; i++) {

                    var item = data[i];
                    if (item != 'rownum') {
                        //var point = { id: item, text: item };
                        var point2 = {label: item, name: item, width: 150, align: "left"};
                        //fieldData.push(point);
                        headData.push(point2);
                    }
                }
                $('#gridtable').jfGridClear();
                $('#gridtable').html("");
                $('#gridtable').removeClass("jfgrid-layout");
                //$('#gridtable').removeAttr("style");

                $('#gridtable').jfGrid({
                    url: config.base_server() + 'dbs/findData?sql=' + sql + '&access_token=' + config.getToken().access_token,
                    headData: headData,
                    isPage: false
                });
                page.search();
            });
        },

        search: function (param) {
            param = param || {};
            $('#gridtable').jfGridSet('reload', param);
        },
    };

    // 保存数据后回调刷新
    refreshGirdData = function () {
        page.search();
    };

    page.init();
}


